﻿using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RTW.OA.Servers
{
    public class MessageServers
    {
        private readonly IRepository<message> MessageRepository;

        public MessageServers()
        {
            MessageRepository = new DapperRepository<message>();
        }

        public IEnumerable<message> GetByUserId(string userName)
        {
            string sql = "SELECT [TO_USER_NAME],[FROM_USER_NAME],[MSG_TYPE],[TITLE],[CONTENT],[URL],[STATUS],[SEND_TIME],[READ_TIME],[MSG_ID] FROM [OADB].[dbo].[message] WHERE [STATUS] = 0 AND [TO_USER_NAME]='" + userName + "' ORDER BY SEND_TIME";
            return MessageRepository.SqlWithParams(sql);
        }

        public int Add(message model)
        {
            return MessageRepository.Insert(model);
        }

        public int UpdateStatus(string msgId)
        {
            string sql = "UPDATE [dbo].[message] SET [STATUS] = 1,[READ_TIME] = GETDATE() WHERE [MSG_ID] = '" + msgId + "'";
            return MessageRepository.InsertUpdateOrDeleteSql(sql);
        }
    }
}